package cn.tedu.csmall.product.repo.impl;

import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.VO.BrandListVO;
import cn.tedu.csmall.product.pojo.VO.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Repository
public class BrandRedisRepositoryImpl implements IBrandRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void save(BrandStandardVO brandStandardVO) {
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        String key = BRAND_ITEM_KEY_PREFIX+brandStandardVO.getId();
        ops.set(key,brandStandardVO);
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(BRAND_ALL_KEY,key);
    }

    @Override
    public void save(List<BrandListVO> brandListVO) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<BrandListVO> list = brandMapper.list();
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        for (BrandListVO item : list) {
            opsForSet.add(BRAND_ALL_KEY,BRAND_LIST_KEY);
            ops.rightPush(BRAND_LIST_KEY,item);
        }
    }

    @Override
    public void deleteAll() {
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        Set<Serializable> serializables = opsForSet.members(BRAND_ALL_KEY);
        Set<String> keys = new HashSet<>();
        for (Serializable serializable : serializables) {
            keys.add((String)serializable);
        }
        //删除缓存中所有数据
//        redisTemplate.delete(BRAND_ALL_KEY);
        redisTemplate.delete(keys);
    }

    @Override
    public BrandStandardVO get(Long id) {
        String key = BRAND_ITEM_KEY_PREFIX+id;
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable result = ops.get(key);
        if (result != null) {
            return (BrandStandardVO) result;
        }
        return null;
    }

    @Override
    public List<BrandListVO> getAll() {
        return getList(0,-1);
    }

    @Override
    public List<BrandListVO> getList(long start, long end) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(BRAND_LIST_KEY, start, end);
        List<BrandListVO> brandListVOS = new ArrayList<>();
        for (Serializable lists : list) {
            brandListVOS.add((BrandListVO) lists);
        }
        return brandListVOS;
    }

}
